package com.zhangxiaofanfan.arthas.db;

import com.zhangxiaofanfan.arthas.pojo.ArthasPO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 模拟数据库操作
 *
 * @author zhangxiaofanfan
 * @date 2025-03-11 10:24:04
 */
@Slf4j
@Component
public class DataCache {
    private static Map<Integer, ArthasPO> map = new HashMap<>();

    static {
        ArthasPO arthas1 = ArthasPO.builder().id(1).name("xiaofan").build();
        ArthasPO arthas2 = ArthasPO.builder().id(2).name("xiaomian").build();
        map.put(arthas1.getId(), arthas1);
        map.put(arthas2.getId(), arthas2);
    }

    /**
     * 条件查询 arthas 对象
     *
     * @param id 主键id
     * @return arthas 对象
     */
    public ArthasPO getArthasCondition(int id) {
        return map.get(id);
    }

    /**
     * 将任务插入到数据库中
     *
     * @param taskId 任务主键id
     * @param arthas 任务对象
     */
    public void insertArthasTask(int taskId, ArthasPO arthas) {
        map.put(taskId, arthas);
        printCache();
    }

    /**
     * 打印缓存数据
     */
    public void printCache() {
        for (Integer taskId : map.keySet()) {
            log.info("task_id[{}] --> arthas[{}]", taskId, map.get(taskId));
        }
    }
}
